package hu.bme.gabor.onlab;

import java.util.ArrayList;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {
    
	private static String serverName;
	private static ArrayList<String> googleAccounts;
	public static String googleAccount;
	
    @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		String[] menuitems = getResources().getStringArray(R.array.menu);

		setListAdapter(new ArrayAdapter<String>(this, R.layout.main, menuitems));

		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView parent, View v, int position, long id) {

				if (id == 0) {
					Intent light = new Intent(getApplicationContext(), Light.class);
					startActivity(light);
				} else if (id == 1) {
					Intent temperature = new Intent(getApplicationContext(), Temperature.class);
					startActivity(temperature);
				} else {
					Toast.makeText(MainActivity.this, id + "", Toast.LENGTH_LONG).show();
				}
			}
		});
	}
    
    @Override
    public void onResume(){
    	super.onResume();
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		serverName = prefs.getString(Settings.PREF_SERVER_NAME, "http://bigbang.dlinkddns.com");
		
		googleAccounts = new ArrayList<String>();
		
		Account[] accounts = AccountManager.get(this).getAccounts();
		for (Account account : accounts) {
		  googleAccounts.add(account.name);
		}
	}
    
    public final static ArrayList<String> getGoogleAccounts(){
    	return googleAccounts;
    }
    
    public final static String getServerName(){
    	return serverName;
    }
    
    public final static void setServerName(String server){
    	serverName = server;
    }
    
 	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.bottommenu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			Intent preferencesIntent = new Intent(this, Settings.class);
			startActivity(preferencesIntent);
			break;

		case R.id.c2dm:
	        Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");  
	        intent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));  
	        intent.putExtra("sender", googleAccount);  
	        startService(intent); 
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}